package baiqi.sishen.community.dto;

import baiqi.sishen.community.execption.CustomizeErrorCode;
import baiqi.sishen.community.execption.CustomizeException;
import lombok.Data;

import java.util.List;

/**
 * 返回结果类型
 *
 * @author 兕神
 * @version 1.0
 * DateTime: 2023/5/1
 */
@Data
public class ResultDTO<T> {
    //状态码
    private Integer code;
    //错误信息
    private String message;
    //数据
    private T data;

    public static ResultDTO error(Integer code, String message) {
        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setCode(code);
        resultDTO.setMessage(message);
        return resultDTO;
    }

    public static ResultDTO success() {
        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setCode(200);
        resultDTO.setMessage("请求成功");
        return resultDTO;
    }

    public static ResultDTO error(CustomizeException e){
        return error(e.getCode(),e.getMessage());
    }

    public static ResultDTO error(CustomizeErrorCode customizeErrorCode){
        return error(customizeErrorCode.getCode(),customizeErrorCode.getMessage());
    }

    public static <T> ResultDTO success(T t) {
        ResultDTO<T> resultDTO = new ResultDTO<>();
        resultDTO.setCode(200);
        resultDTO.setData(t);
        resultDTO.setMessage("请求成功");
        return resultDTO;
    }
}
